<template>
	<view class="container">
		<view class="list-header">
			<text class="header-item">积分兑换名称</text>
			<text class="header-item">增/减积分</text>
			<text class="header-item">使用时间</text>
			<text class="header-item">使用人</text>
		</view>
		<scroll-view class="list-content" scroll-y>
			<view v-for="(item, index) in listData" :key="index" class="list-item">
				<text class="item-name">{{item.change_name}}</text>
				<text class="item-points" :class="{'increase': item.score > 0, 'decrease': item.score < 0}">
					{{item.score > 0 ? '+' + item.score : item.score}}
				</text>
				<text class="item-time">{{timeLoad(item.create_time)}}</text>
				<text class="item-name">{{item.user_name}}</text>

			</view>
		</scroll-view>
	</view>
	<uni-load-more :status="status" :content-text="contentText" @click="onLoadList()" />
</template>

<script>
	import {
		publicScoreList
	} from '@/api/api.js'
	import {
		formatDate
	} from '@/utils/time.js'
	import store from '@/store/index.js'; //需要引入store
	export default {
		data() {
			return {
				listData: [],
				status: 'more',
				contentText: {
					contentdown: '点击查看更多',
				},
				listReq: {
					pageInfo: {
						page: 1,
						size: 5
					}
				}
			};
		},
		onLoad() {
			this.loadList()
		},
		methods: {
			onLoadList() {
				this.status = 'loading'
				this.listReq.pageInfo.page += 1
				this.loadList()
			},
			loadList() {
				publicScoreList(this.listReq).then(res => {
					console.log(res.data)
					if (res.data.code != 0) {
						this.listReq.pageInfo.page -= 1
						this.status = 'noMore'
						uni.showToast({
							icon: "none",
							title: res.data.message
						})
					} else {
						var data = res.data.data
						let list = this.listData
						this.listData = list.concat(data.list)
						if (data.page_info.total_page - this.listReq.pageInfo.page <= 0) {
							this.status = 'noMore'
						} else {
							this.status = 'more'
						}
					}
				})
			},
			timeLoad(time) {
				const d = new Date(time * 1000)
				return formatDate(d, 'yyyy-MM-dd')
			},
		}
	};
</script>

<style scoped>
	.container {
		padding: 20rpx;
	}

	.list-header {
		display: flex;
		flex-direction: row;
		justify-content: space-between;
		font-size: 24rpx;
		font-weight: bold;
		margin-bottom: 20rpx;
	}

	.header-item {
		flex: 1;
		text-align: center;
	}

	.list-content {


		border-radius: 4rpx;
		padding: 20rpx;
	}

	.list-item {
		display: flex;
		flex-direction: row;
		align-items: center;
		margin-bottom: 10rpx;
	}

	.item-title {
		flex: 1;
	}

	.item-points {
		flex: 1;
		text-align: center;
	}

	.increase {
		color: #f00;
	}

	.decrease {
		color: #0f0;
	}

	.item-name {
		flex: 1;
	}

	.item-time {
		flex: 2;
	}
</style>